package core;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

public class Monster extends Actor {
	private static final ArrayList<Character> MOVES = new ArrayList<Character>(
			Arrays.asList('u', 'i', 'o', 'j', 'k', 'l', 'm', ',', '.'));

	public Monster() {
		super();
		this.setVisible(false);
	}

	public Monster(Room room) {
		this();
		this.place(room);
	}

	public Monster(int pos) {
		super(pos);
		this.setVisible(false);
	}

	@Override
	public boolean usePotion(Potion p, Room room) {
		return intUsePotion(p, room);
	}

	public void selfMovement(Room room) {
		intSelfMovement(room);
	}

	private char intMoveDirection() {
		Random rand = new Random();
		return MOVES.get(rand.nextInt(MOVES.size()));
	}

	/* If there's a player near the monster, the mob moves to him */
	private void intSelfMovement(Room room) {
		if (room.getElement((this.getPos() / room.getLongSide()) - 1,
				(this.getPos() % room.getLongSide()) - 1) instanceof Player)
			this.move('u', room);
		else if (room.getElement((this.getPos() / room.getLongSide()) - 1,
				this.getPos() % room.getLongSide()) instanceof Player)
			this.move('i', room);
		else if (room.getElement((this.getPos() / room.getLongSide()) - 1,
				(this.getPos() % room.getLongSide()) + 1) instanceof Player)
			this.move('o', room);
		else if (room.getElement((this.getPos() / room.getLongSide()),
				(this.getPos() % room.getLongSide()) - 1) instanceof Player)
			this.move('j', room);
		else if (room.getElement(this.getPos() / room.getLongSide(),
				(this.getPos() % room.getLongSide())) instanceof Player)
			this.move('k', room);
		else if (room.getElement(this.getPos() / room.getLongSide(),
				(this.getPos() % room.getLongSide()) + 1) instanceof Player)
			this.move('l', room);
		else if (room.getElement((this.getPos() / room.getLongSide()) + 1,
				(this.getPos() % room.getLongSide()) - 1) instanceof Player)
			this.move('m', room);
		else if (room.getElement((this.getPos() / room.getLongSide()) + 1,
				this.getPos() % room.getLongSide()) instanceof Player)
			this.move(',', room);
		else if (room.getElement((this.getPos() / room.getLongSide()) + 1,
				(this.getPos() % room.getLongSide()) + 1) instanceof Player)
			this.move('.', room);
		else
			while (true)
				if (this.move(this.intMoveDirection(), room)) {
					if (this.getPos() == room.getEntrance().getPos()
							|| this.getPos() == room.getExit().getPos())
						continue;
					else
						break;
				}
	}

	private boolean intUsePotion(Potion p, Room room) {
		if (this.getPos() == p.getPos()) {
			room.removeElement(p);
			return true;
		}
		return false;
	}

	@Override
	public String toString() {
		return "Monster []" + super.toString();
	}
}
